package com.liuzh.kaoyanspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuzh.kaoyanspringboot.common.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuzh.kaoyanspringboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.liuzh.kaoyanspringboot.service.ConcernService;
import com.liuzh.kaoyanspringboot.entity.Concern;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘志鸿
 * @since 2023-04-11
 */
@RestController
@RequestMapping("/concern")
public class ConcernController {

    @Autowired
    private ConcernService concernService;

    // 新增或更新
    @PostMapping
    public Result save(@RequestBody Concern concern) {
        return Result.success(concernService.mySave(concern));
    }

    // 根据 concernId 删除
    @DeleteMapping("/{concernId}")
    public Result deleteByConcern(@PathVariable Integer concernId) {
        return Result.success(concernService.removeConcern(TokenUtils.getCurrentUser().getId(),concernId));
    }
    @DeleteMapping("/delete/{concernId}")
    public Result delete(@PathVariable Integer concernId) {
//        System.out.println(concern.getMyId()+ "======== "+concern.getConcernId());
        return Result.success(concernService.removeConcern(TokenUtils.getCurrentUser().getId(), concernId));
//        return Result.success(true);
    }

    // 批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(concernService.removeBatchByIds(ids));
    }

    //查询所有数据
    @GetMapping
    public Result findAll() {
        return Result.success(concernService.list());
    }

    // 根据 用户id 查询他的关注对象
    @GetMapping("/users/{myId}")
    public Result findAdminConcern(@PathVariable Integer myId) {
        return Result.success(concernService.getUsersByMyId(myId));
    }

    /**
     * 根据 userId 获取 该用户的关注
     * @param userId
     * @return
     */
    @GetMapping("/article/{userId}")
    public Result findConcernArticle(@PathVariable Integer userId) {
        return Result.success(concernService.findConcernArticle(userId));
    }

    @GetMapping("/isConcern/{concernId}")
    public Result isConcern(@PathVariable Integer concernId) {
        return Result.success(concernService.findConcern(concernId, TokenUtils.getCurrentUser().getId()));
    }

    //简单的分页查询
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Concern> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(concernService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }


}

